/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.gui.security;

import it.polimi.registration.business.security.boundary.EventManager;
import it.polimi.registration.business.security.boundary.UserManager;
import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.User;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author miglie
 */
@Named
@RequestScoped
public class EventCreationBean {
    
	@Inject
    private Logger logger;

	@EJB
	private UserManager um;
	
	@EJB
	private EventManager em;

	private Event event;
	private List<User> allUsers;

	public EventCreationBean() {
	}
	
	public Event getEvent() {
		if(event == null)
			event = new Event(um.getLoggedUser());
		
		return event;
	}
	
	public void setEvent(Event event) {
		this.event = event;
	}
	
	public String create() {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
			em.save(event);
			return "/index.xhtml?faces-redirect=true";
		} catch(Exception e) {
	        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Creation Failed","Creation Failed"));
	        logger.log(Level.SEVERE, "Creation Failed");
			return null;
		}
	}
	
	public List<User> getAllUsers() {
		if(this.allUsers == null)
			this.allUsers = um.getAllUsers();
		
		return allUsers;
	}
	
	public void setAllUsers(List<User> allUsers) {
		this.allUsers = allUsers;
	}

	/*public User getUser() {
		if (user == null) {
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String register() {
        FacesContext context = FacesContext.getCurrentInstance();
        
		try {
			um.save(user);
			return "/index.xhtml?faces-redirect=true";
		} catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Registering Failed","Registering Failed"));
            logger.log(Level.SEVERE,"Register Failed");
            return null;
		}
	}*/
}
